package com.jidu.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jidu.common.core.domain.BaseEntity;
import com.jidu.common.utils.DateUtils;
import com.jidu.common.utils.SecurityUtils;
import com.jidu.common.utils.StringUtils;
import com.jidu.domain.*;
import com.jidu.domain.dto.ApplyStudentDto;
import com.jidu.mapper.JpChangeClassRecordMapper;
import com.jidu.mapper.SysClassMapper;
import com.jidu.mapper.SysCourseMapper;
import com.jidu.mapper.SysTeacherMapper;
import com.jidu.service.ICourseStudentRelService;
import com.jidu.service.IJpChangeClassRecordService;
import com.jidu.service.ISysStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


/**
 * 转班记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-24
 */
@Service
public class JpChangeClassRecordServiceImpl  extends ServiceImpl<JpChangeClassRecordMapper, JpChangeClassRecord> implements IJpChangeClassRecordService
{
    @Autowired
    private JpChangeClassRecordMapper jpChangeClassRecordMapper;
    @Autowired
    private ISysStudentService sysStudentService;
    @Autowired
    private ICourseStudentRelService courseStudentRelService;
    @Autowired
    private SysClassMapper sysClassMapper;
    @Autowired
    private SysTeacherMapper sysTeacherMapper;
    @Autowired
    private SysCourseMapper sysCourseMapper;

    /**
     * 查询转班记录
     * 
     * @param id 转班记录主键
     * @return 转班记录
     */
    @Override
    public JpChangeClassRecord selectJpChangeClassRecordById(Long id)
    {
        return jpChangeClassRecordMapper.selectJpChangeClassRecordById(id);
    }

    /**
     * 查询转班记录列表
     * 
     * @param jpChangeClassRecord 转班记录
     * @return 转班记录
     */
    @Override
    public List<JpChangeClassRecord> selectJpChangeClassRecordList(JpChangeClassRecord jpChangeClassRecord)
    {
        return jpChangeClassRecordMapper.selectJpChangeClassRecordList(jpChangeClassRecord);
    }

    /**
     * 新增转班记录
     * 
     * @param jpChangeClassRecord 转班记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJpChangeClassRecord(JpChangeClassRecord jpChangeClassRecord)
    {
        ApplyStudentDto studentCourse = sysStudentService.getStudentCourse(jpChangeClassRecord.getStudentId(), jpChangeClassRecord.getOldCourseStudentId());
        if (studentCourse == null){
            throw new RuntimeException("该课程信息不存在");
        }
        if ("1".equals(studentCourse.getIsChangeClass())){
            throw new RuntimeException("该记录已转班");
        }
        Long courseStudentId = studentCourse.getCourseStudentId();
        CourseStudentRel rel = new CourseStudentRel();
        rel.setId(courseStudentId);
        rel.setIsChangeClass("1");
        courseStudentRelService.updateCourseStudentRel(rel);

        //插入一条新纪录
        CourseStudentRel r = new CourseStudentRel();
        r.setStudentId(studentCourse.getStudentId());
        if (jpChangeClassRecord.getCourseId() != null){
            r.setCourseId(jpChangeClassRecord.getCourseId());
        }else {
            r.setCourseId(studentCourse.getCourseId());
        }
        if (jpChangeClassRecord.getClassId() != null){
            r.setClassId(jpChangeClassRecord.getClassId());
        }else {
            r.setClassId(studentCourse.getClassId());
        }
        if (jpChangeClassRecord.getTeacherId() != null){
            r.setTeacherId(jpChangeClassRecord.getTeacherId());
        }else {
            r.setTeacherId(studentCourse.getTeacherId());
        }
        if (jpChangeClassRecord.getPrice() != null){
            r.setPrice(jpChangeClassRecord.getPrice());
        }else {
            r.setPrice(studentCourse.getPrice());
        }
        r.setCreateTime(studentCourse.getCreateTime());
        r.setUpdateTime(new Date());
        r.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        r.setNum(studentCourse.getNumber());
        r.setResNum(studentCourse.getResNum());
        r.setStatus(studentCourse.getStatus());
        r.setRemark(studentCourse.getRemark());
        r.setLeaveTimes(studentCourse.getLeaveTimes());
        r.setIsChangeClass("2");//已完成转班
        r.setOrgId(1L);
        courseStudentRelService.insertCourseStudentRel(r);


        jpChangeClassRecord.setCreateTime(DateUtils.getNowDate());
        jpChangeClassRecord.setOldCourseStudentId(studentCourse.getCourseStudentId());
        jpChangeClassRecord.setCourseStudentId(r.getId());
        jpChangeClassRecord.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        jpChangeClassRecord.setStudentId(jpChangeClassRecord.getStudentId());
        JpChangeClassRecord one = this.lambdaQuery().eq(JpChangeClassRecord::getCourseStudentId, studentCourse.getCourseStudentId())
                .eq(JpChangeClassRecord::getStudentId, jpChangeClassRecord.getStudentId()).one();
        if (one != null){
            jpChangeClassRecord.setDescription(one.getDescription());
        }else {
            String s = IdUtil.fastSimpleUUID();
            jpChangeClassRecord.setDescription(s);
        }
        return jpChangeClassRecordMapper.insertJpChangeClassRecord(jpChangeClassRecord);
    }

    public static void main(String[] args) {
        System.out.println();
    }

    /**
     * 修改转班记录
     * 
     * @param jpChangeClassRecord 转班记录
     * @return 结果
     */
    @Override
    public int updateJpChangeClassRecord(JpChangeClassRecord jpChangeClassRecord)
    {
        return jpChangeClassRecordMapper.updateJpChangeClassRecord(jpChangeClassRecord);
    }

    /**
     * 批量删除转班记录
     * 
     * @param ids 需要删除的转班记录主键
     * @return 结果
     */
    @Override
    public int deleteJpChangeClassRecordByIds(Long[] ids)
    {
        return jpChangeClassRecordMapper.deleteJpChangeClassRecordByIds(ids);
    }

    @Override
    public List<Map<String, Object>> changeList(Long studentId, Long courseStudentId) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        CourseStudentRel courseStudentRel = courseStudentRelService.selectCourseStudentRelById(courseStudentId);
        if (courseStudentRel != null && "2".equals(courseStudentRel.getIsChangeClass())){
            JpChangeClassRecord one = this.lambdaQuery().eq(JpChangeClassRecord::getStudentId, studentId).eq(JpChangeClassRecord::getCourseStudentId, courseStudentId).one();
            if (one != null){
                String description = one.getDescription();
                List<JpChangeClassRecord> list = this.lambdaQuery().eq(JpChangeClassRecord::getDescription, description).orderByDesc(JpChangeClassRecord::getCreateTime).list();
                if (!CollectionUtils.isEmpty(list)){
                    for (JpChangeClassRecord cr : list) {
                        Map<String, Object> hashMap = new HashMap<>();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        hashMap.put("createTime",format.format(cr.getCreateTime()));
                        Long courseStudentId1 = cr.getCourseStudentId();
                        CourseStudentRel courseStudentRel1 = courseStudentRelService.selectCourseStudentRelById(courseStudentId1);
                        if (courseStudentRel1 != null){
                            Long classId = courseStudentRel1.getClassId();
                            Long teacherId = courseStudentRel1.getTeacherId();
                            Long courseId = courseStudentRel1.getCourseId();
                            SysClass sysClass = sysClassMapper.selectSysClassById(classId);
                            if (sysClass != null){
                                hashMap.put("className",sysClass.getName());
                            }
                            SysCourse sysCourse = sysCourseMapper.selectSysCourseById(courseId);
                            if (sysCourse != null){
                                hashMap.put("courseName",sysCourse.getName());
                            }
                            SysTeacher sysTeacher = sysTeacherMapper.selectSysTeacherById(teacherId);
                            if (sysTeacher != null){
                                hashMap.put("teacherName",sysTeacher.getName());
                            }
                        }
                        resultList.add(hashMap);
                    }
                }
            }
        }
        return resultList;
    }

    /**
     * 删除转班记录信息
     * 
     * @param id 转班记录主键
     * @return 结果
     */
    @Override
    public int deleteJpChangeClassRecordById(Long id)
    {
        return jpChangeClassRecordMapper.deleteJpChangeClassRecordById(id);
    }
}
